package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.comm.AccessClose;
import com.cndw.kungfu.comm.CrossCommand;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Goods;
import com.cndw.kungfu.domain.Pet;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LangConfigLib;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.CrossProxySerialize;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.model.DataStaticCenter;
import com.cndw.kungfu.model.activity.domain.BalefileStaticeData;
import com.cndw.kungfu.model.activity.domain.CrossServer;
import com.cndw.kungfu.model.activity.holiday.MMFlowerData;
import com.cndw.kungfu.model.activity.holiday.MMFlowerLog;
import com.cndw.kungfu.model.activity.holiday.MidAutumnData;
import com.cndw.kungfu.model.activity.holiday.MidAutumnParams;
import com.cndw.kungfu.model.disciple.PlayerMartial;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.disciple.domain.DiscipleCompararor;
import com.cndw.kungfu.model.goods.domain.ModeGoodsStarSuit;
import com.cndw.kungfu.model.goods.domain.SubPops;
import com.cndw.kungfu.model.goods.domain.SysModeAwards;
import com.cndw.kungfu.model.pvp.campcross.CampCrossArea;
import com.cndw.kungfu.model.pvp.campcross.CampCrossCore;
import com.cndw.kungfu.model.pvp.campcross.CampCrossReport;
import com.cndw.kungfu.model.pvp.campcross.CampCrossReportBattle;
import com.cndw.kungfu.model.pvp.campcross.CampTeam;
import com.cndw.kungfu.model.pvp.war.WarBet;
import com.cndw.kungfu.model.pvp.war.WarConfig;
import com.cndw.kungfu.model.pvp.war.WarCoreCenter;
import com.cndw.kungfu.model.pvp.war.WarCrossData;
import com.cndw.kungfu.model.pvp.war.WarData;
import com.cndw.kungfu.model.pvp.war.WarFighter;
import com.cndw.kungfu.model.pvp.war.WarPlayer;
import com.cndw.kungfu.model.pvp.war.WarVs;
import com.cndw.kungfu.model.pvp.war.WarWorldData;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.DiscipleSmallVO;
import com.cndw.kungfu.vo.PlayerChangeVO;
import com.cndw.kungfu.vo.PlayerReceiveVO;
import com.cndw.kungfu.vo.battlevo.TeamCampVO;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;
import com.cndw.rpg.framework.SessionHolder;

@Controller
@CrossCommand
public class CrossController {

	/**
	 * 跨服发送广播消息
	 * 
	 * @param session
	 * @param sign
	 * @param message
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage crossChat(Session session, String sign, String message) {

		ChatMain.systemMessage(message);

		return ResponseMessage.getSuccMessage(1);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage crossInfo(Session session, String sign, long uid, int type) {
		if (type == 0) {
			return crossPlayerInfo(session, sign, uid);
		} else if (type == 1) {
			return crossPlayerDisciples(session, sign, uid);
		} else if (type == 2) {
			return crossPlayerPet(session, sign, uid);
		} else {
			return ResponseMessage.getSuccMessage(Collections.emptyMap());
		}
	}

	@UnAuth
	@GmCommand
	public ResponseMessage crossPlayerInfo(Session session, String sign, long uid) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return ResponseMessage.getSuccMessage(false);
		}
		Collection<Goods> equipGoods = target.getPlayerBoxs().getEquipList();
		List<GoodsVO> equipVOList = new ArrayList<GoodsVO>();
		for (Goods e : equipGoods) {
			equipVOList.add(e.toGoodsVO());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("equipList", equipVOList);
		map.put("goodsStarSuit", ModeGoodsStarSuit.getStarSuite(equipGoods));
		map.put("otherPlayer", target.toOtherPlayerVO());
		map.put("challengeRanking", DataSerialize.getInstance().getChallengeData().getOrder(target.getPlayerId()));
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage crossPlayerDisciples(Session session, String sign, long uid) {
		Player player = Context.getPlayerServer().getByPlayerId(uid);
		if (player == null) {
			return null;
		}
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(uid);
		if (playerMartial == null) {
			return null;
		}
		int score = 0;

		List<DiscipleSmallVO> vos = new ArrayList<DiscipleSmallVO>();
		ArrayList<Disciple> list = new ArrayList<Disciple>(playerMartial.getDisciples());
		Collections.sort(list, new DiscipleCompararor());

		for (Disciple e : list) {
			if (e.getCampId() <= 0) {
				continue;
			}
			score += e.getScore();

			vos.add(e.toSmallVO());
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("smallDisciples", vos);
		map.put("score", score);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage crossPlayerPet(Session session, String sign, long uid) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return null;
		}
		Pet pet = target.getDataInfo().getPet();
		if (pet == null) {
			return ResponseMessage.getSuccMessage(false);
		}

		return ResponseMessage.getSuccMessage(pet.toVO());
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warPlayers(Session session, String sign) {
		ConcurrentHashMap<Integer, Map<Integer, Long>> winerMap = WarData.getInstance().getWinerMap();
		List<WarPlayer> warPlayers = new ArrayList<WarPlayer>();
		if (winerMap.containsKey(32)) {
			Collection<Long> ids = winerMap.get(32).values();
			for (Long pid : ids) {
				Player target = Context.getPlayerServer().getByPlayerId(pid);
				if (target == null) {
					continue;
				}
				WarPlayer warPlayer = new WarPlayer();
				warPlayer.setPid(target.getPlayerId());
				warPlayer.setPn(target.getPlayerName());
				warPlayer.setPp(target.getScore());
				warPlayer.setPv(target.getVocationId());
				warPlayer.setSid(ConstantsConfig.getSysCgServiceId());
				warPlayer.setPl(target.getLevel());

				warPlayers.add(warPlayer);
			}
		} else {

		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sid", ConstantsConfig.getSysCgServiceId());
		map.put("warPlayers", warPlayers);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warFighter(Session session, String sign, long targetId) {
		Player player = Context.getPlayerServer().getByPlayerId(targetId);
		if (player == null) {
			return ResponseMessage.getFailMessage(false);
		}
		WarFighter warFighter = new WarFighter(player);
		return ResponseMessage.getSuccMessage(warFighter);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warFighters(Session session, String sign, String idstr) {
		List<WarFighter> warFighters = new ArrayList<WarFighter>();
		try {
			long[] ids = StringProcesser.toLongArray(idstr);
			for (long uid : ids) {
				Player player = Context.getPlayerServer().getByPlayerId(uid);
				if (player == null) {
					continue;
				}
				WarFighter warFighter = new WarFighter(player);
				warFighters.add(warFighter);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.getMain().debug("cross get err " + idstr);
		}

		return ResponseMessage.getSuccMessage(warFighters);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warPlayer(Session session, String sign, long uid) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return ResponseMessage.getFailMessage(false);
		}
		WarPlayer warPlayer = new WarPlayer();
		warPlayer.setPid(target.getPlayerId());
		warPlayer.setPn(target.getPlayerName());
		warPlayer.setPp(target.getScore());
		warPlayer.setPv(target.getVocationId());
		warPlayer.setSid(ConstantsConfig.getSysCgServiceId());
		warPlayer.setPl(target.getLevel());
		return ResponseMessage.getSuccMessage(warPlayer);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warShow(Session session, String sign, int serverId) {

		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Map<String, Object> map = new HashMap<String, Object>();
		int step = warCrossData.getStep();
		if (step == 2 && !warCrossData.isProm()) {
			step = 1;
		}
		map.put("step", step);
		map.put("camType", warCrossData.getCamType());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warVs(Session session, String sign, int serverId, long sourceId) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Iterator<WarVs> it = warCrossData.getVsIterator();
		List<Object[]> vsList = new ArrayList<Object[]>();
		int diff = TimeProcesser.getUnixTime() - WarConfig.getCrossBegin() - WarConfig.eliminateBegin;
		int k = 0;
		while (it.hasNext()) {
			WarVs warVs = (WarVs) it.next();
			if (warVs == null) {
				continue;
			}
			long t = warVs.getS() == sourceId ? warVs.getT() : warVs.getT() == sourceId ? warVs.getS() : 0;
			if (t > 0) {
				WarPlayer target = warCrossData.getWarPlayer(t);
				boolean win = warVs.getS() == sourceId ? warVs.getW() == 1 : warVs.getW() == 2;

				if (target != null) {
					vsList.add(new Object[] { target.getPid(), target.getPn(), warVs.getW() == 0 ? 0 : win ? 1 : 2, target.getSid() });
				}
				k++;
				if (diff <= k * WarConfig.emtOneTime) {
					break;
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vsList", vsList);
		map.put("emtNum", warCrossData.getEmtNum(k));

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warPromotion(Session session, String sign, int serverId, long uid) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);

		Map<Integer, Map<Integer, Long>> winerMap = warCrossData.getWinerMap();
		Map<Integer, Map<Integer, Object[]>> view = new HashMap<Integer, Map<Integer, Object[]>>();
		for (int i = 1; i < WarConfig.camTypeList.length; i++) {
			int cmType = WarConfig.camTypeList[i];
			if (winerMap.containsKey(cmType)) {
				HashMap<Integer, Object[]> camWiner = new HashMap<Integer, Object[]>();
				view.put(cmType, camWiner);
				Map<Integer, Long> erMap = winerMap.get(cmType);
				Iterator<Entry<Integer, Long>> it = erMap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<Integer, Long> entry = (Map.Entry<Integer, Long>) it.next();
					WarPlayer target = warCrossData.getWarPlayer(entry.getValue());
					if (target == null) {
						LogUtil.getMain().debug("err xxxx" + entry.getValue());
					}
					camWiner.put(entry.getKey(),
							new Object[] { target.getPid(), target.getPn(), target.getPv(), target.getPl(), target.getPp(), target.getSid() });
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("view", view);
		int camType = warCrossData.getCamType();
		map.put("batOnceAsset", WarConfig.batOnceAsset.containsKey(camType) ? WarConfig.batOnceAsset.get(camType) : 0);

		WarBet warBet = warCrossData.betGet(uid);
		if (warBet != null && warBet.getC() <= camType) {
			map.put("betTarget", warCrossData.betGet(uid));
			WarPlayer warPlayer = warCrossData.getWarPlayer(warBet.getT());
			map.put("betName", warPlayer.getPn());
			map.put("betSid", warPlayer.getSid());
		}
		map.put("canBet", warCrossData.isBet());

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warPromotionvs(Session session, String sign, int serverId, long sourceId, long targetId) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Iterator<WarVs> it = warCrossData.getOrderVsList();
		List<Object[]> vsList = new ArrayList<Object[]>();
		while (it.hasNext()) {
			WarVs warVs = (WarVs) it.next();
			if ((sourceId == warVs.getS() && targetId == warVs.getT()) || (sourceId == warVs.getT() && targetId == warVs.getS())) {
				WarPlayer source = warCrossData.getWarPlayer(warVs.getS());
				WarPlayer target = warCrossData.getWarPlayer(warVs.getT());
				if (source != null && target != null) {
					vsList.add(new Object[] { source.getPid(), source.getPn(), target.getPid(), target.getPn(), warVs.getW(), source.getSid(), target.getSid() });
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vsList", vsList);
		map.put("camType", warCrossData.getCamType());
		return ResponseMessage.getSuccMessage(map);

	}

	@UnAuth
	@GmCommand
	public ResponseMessage warPromotionMy(Session session, String sign, int serverId, long uid) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Iterator<WarVs> it = warCrossData.getOrderVsList();
		List<Object[]> vsList = new ArrayList<Object[]>();
		while (it.hasNext()) {
			WarVs warVs = (WarVs) it.next();

			long t = warVs.getS() == uid ? warVs.getT() : warVs.getT() == uid ? warVs.getS() : 0;
			if (t > 0) {
				WarPlayer target = warCrossData.getWarPlayer(t);
				boolean win = warVs.getS() == uid ? warVs.getW() == 1 : warVs.getW() == 2;
				vsList.add(new Object[] { target.getPid(), target.getPn(), warVs.getW() == 0 ? 0 : win ? 1 : 2, warVs.getC(), target.getSid() });
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vsList", vsList);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warOrderPower(Session session, String sign, int serverId) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("orderPower", warCrossData.getOrderPower());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warBet(Session session, String sign, int serverId, long uid, int camType, long targetId, int asset) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Map<String, Object> map = new HashMap<String, Object>();
		WarPlayer warPlayer = warCrossData.getWarPlayer(targetId);
		WarBet oldBet = warCrossData.betGet(uid);

		if (warPlayer == null || !warCrossData.isBet() || warCrossData.getCamType() != camType || (oldBet != null && oldBet.getC() == camType)) {
			map.put("ret", false);
		} else {

			WarBet warBet = warCrossData.betAsset(camType, serverId, uid, targetId, asset);
			warCrossData.commitSync();
			map.put("ret", true);
			map.put("betTarget", warBet);
			map.put("betName", warPlayer.getPn());
			map.put("betSid", warPlayer.getSid());
		}
		map.put("canBet", warCrossData.isBet());

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warBetList(Session session, String sign, int serverId, long uid) {
		WarCrossData warCrossData = WarCoreCenter.getDataBySid(serverId);
		Iterator<WarBet> bets = warCrossData.getBets();
		List<Object[]> myBets = new ArrayList<Object[]>();
		while (bets.hasNext()) {
			WarBet warBet = (WarBet) bets.next();
			if (warBet.getS() == uid) {
				WarPlayer target = warCrossData.getWarPlayer(warBet.getT());
				myBets.add(new Object[] { warBet.getC(), warBet.getT(), target.getPn(), warBet.getAsset(), warBet.getW(), target.getSid() });
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bets", myBets);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warCrossFlower(Session session, String sign, int serverId, long uid, String pn, int type) {
		WarCoreCenter.flower(uid, pn, type, serverId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", true);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warFlowerList(Session session, String sign, int serverId) {
		int area = CrossServer.getArea(serverId);
		area = area <= 0 ? 1 : area;// 针对测试服异常情况
		WarPlayer boss = CrossProxySerialize.getInstance().getCrossBoss(area);
		while (boss == null && area > 1) {
			area--;
			boss = CrossProxySerialize.getInstance().getCrossBoss(area);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flowers", CrossProxySerialize.getInstance().getWarFlowers());
		map.put("boss", boss);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warApiCam(Session session, String sign, int step, int camType) {
		CrossProxySerialize.getInstance().stateWar(1, step, camType);
		CrossProxySerialize.getInstance().commitSync();
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warAward(Session session, String sign, long uid, int camType) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return ResponseMessage.getSuccMessage(true);
		}
		if (!CrossProxySerialize.getInstance().awardAdd(uid, camType)) {
			return ResponseMessage.getSuccMessage(true);
		}
		int[] award = WarConfig.awardMap.get(camType);
		int gNum = award[2];
		int aNum = award[3];
		String title = LanguageLib.get("war.award.title");
		String content = LanguageLib.get("war.award.content", WarConfig.getName(camType), gNum, aNum);
		List<SubPops> attachment = new ArrayList<SubPops>();
		attachment.add(new SubPops(WarConfig.heroGoodsId, gNum, 0, 0));
		attachment.add(new SubPops(0, 0, aNum, 0));

		Context.getMessageServer().sendSysMail(target.getPlayerId(), target.getPlayerName(), title, content, attachment);
		if (award[7] > 0) {
			Context.getGeneralServer().addTitle(uid, award[7]);
		}
		LogServer.recordLog(target, ConstantsLog.activity, 90303, gNum, aNum, camType, 2);
		LogUtil.getMain().debug("award " + uid + " c " + camType);
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage warBetAward(Session session, String sign, int type, long uid, int camType, boolean suc, int asset, String content) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return ResponseMessage.getSuccMessage(true);
		}
		if (type == 1) {
			if (!CrossProxySerialize.getInstance().betAward(uid, camType)) {
				return ResponseMessage.getSuccMessage(true);
			}
		} else {
			if (!CrossProxySerialize.getInstance().betAwardWorld(uid, camType)) {
				return ResponseMessage.getSuccMessage(true);
			}
		}

		String title = LanguageLib.get("war.bet.title");
		if (suc) {
			target.alterAsset(asset);
			target.commitSync();
			Context.getPlayerServer().change(PlayerChangeVO.PLAYER_ASSET, target.getAsset(), uid);
			LogServer.recordLog(target, ConstantsLog.activity, 90302, asset, camType);
		}

		Context.getMessageServer().recordEvent(target.getPlayerId(), title, content, Constants.eventTypeWarBet, 5);
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldVs(Session session, String sign, long sourceId) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();
		Iterator<WarVs> it = warWorldData.getVsIterator();
		List<Object[]> vsList = new ArrayList<Object[]>();
		int diff = TimeProcesser.getUnixTime() - WarConfig.getWorldBegin() - WarConfig.eliminateBegin;
		int k = 0;
		while (it.hasNext()) {
			WarVs warVs = (WarVs) it.next();
			if (warVs == null) {
				continue;
			}
			long t = warVs.getS() == sourceId ? warVs.getT() : warVs.getT() == sourceId ? warVs.getS() : 0;
			if (t > 0) {
				WarPlayer target = warWorldData.getWarPlayer(t);
				boolean win = warVs.getS() == sourceId ? warVs.getW() == 1 : warVs.getW() == 2;

				if (target != null) {
					vsList.add(new Object[] { target.getPid(), target.getPn(), warVs.getW() == 0 ? 0 : win ? 1 : 2, target.getSid() });
				}
				k++;
				if (diff <= k * WarConfig.emtOneTime) {
					break;
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vsList", vsList);
		map.put("emtNum", warWorldData.getEmtNum(k));

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldPromotion(Session session, String sign, long uid) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();

		Map<Integer, Map<Integer, Long>> winerMap = warWorldData.getWinerMap();
		Map<Integer, Map<Integer, Object[]>> view = new HashMap<Integer, Map<Integer, Object[]>>();
		for (int i = 1; i < WarConfig.camTypeList.length; i++) {
			int cmType = WarConfig.camTypeList[i];
			if (winerMap.containsKey(cmType)) {
				HashMap<Integer, Object[]> camWiner = new HashMap<Integer, Object[]>();
				view.put(cmType, camWiner);
				Map<Integer, Long> erMap = winerMap.get(cmType);
				Iterator<Entry<Integer, Long>> it = erMap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<Integer, Long> entry = (Map.Entry<Integer, Long>) it.next();
					WarPlayer target = warWorldData.getWarPlayer(entry.getValue());
					if (target == null) {
						LogUtil.getMain().debug("err xxxx" + entry.getValue());
					}
					camWiner.put(entry.getKey(),
							new Object[] { target.getPid(), target.getPn(), target.getPv(), target.getPl(), target.getPp(), target.getSid() });
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("view", view);
		int camType = warWorldData.getCamType();
		map.put("batOnceAsset", WarConfig.batOnceAsset.containsKey(camType) ? WarConfig.batOnceAsset.get(camType) : 0);

		WarBet warBet = warWorldData.betGet(uid);
		if (warBet != null && warBet.getC() <= camType) {
			map.put("betTarget", warWorldData.betGet(uid));
			WarPlayer warPlayer = warWorldData.getWarPlayer(warBet.getT());
			map.put("betName", warPlayer.getPn());
			map.put("betSid", warPlayer.getSid());
		}
		map.put("canBet", warWorldData.isBet());

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldPromotionvs(Session session, String sign, long sourceId, long targetId) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();
		Iterator<WarVs> it = warWorldData.getOrderVsList();
		List<Object[]> vsList = new ArrayList<Object[]>();
		while (it.hasNext()) {
			WarVs warVs = (WarVs) it.next();
			if ((sourceId == warVs.getS() && targetId == warVs.getT()) || (sourceId == warVs.getT() && targetId == warVs.getS())) {
				WarPlayer source = warWorldData.getWarPlayer(warVs.getS());
				WarPlayer target = warWorldData.getWarPlayer(warVs.getT());
				if (source != null && target != null) {
					vsList.add(new Object[] { source.getPid(), source.getPn(), target.getPid(), target.getPn(), warVs.getW(), source.getSid(), target.getSid() });
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vsList", vsList);
		map.put("camType", warWorldData.getCamType());
		return ResponseMessage.getSuccMessage(map);

	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldPromotionMy(Session session, String sign, long uid) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();
		Iterator<WarVs> it = warWorldData.getOrderVsList();
		List<Object[]> vsList = new ArrayList<Object[]>();
		while (it.hasNext()) {
			WarVs warVs = (WarVs) it.next();

			long t = warVs.getS() == uid ? warVs.getT() : warVs.getT() == uid ? warVs.getS() : 0;
			if (t > 0) {
				WarPlayer target = warWorldData.getWarPlayer(t);
				boolean win = warVs.getS() == uid ? warVs.getW() == 1 : warVs.getW() == 2;
				vsList.add(new Object[] { target.getPid(), target.getPn(), warVs.getW() == 0 ? 0 : win ? 1 : 2, warVs.getC(), target.getSid() });
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vsList", vsList);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldOrderPower(Session session, String sign) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderPower", warWorldData.getOrderPower());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldBet(Session session, String sign, int serverId, long uid, int camType, long targetId, int asset) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();
		Map<String, Object> map = new HashMap<String, Object>();
		WarPlayer warPlayer = warWorldData.getWarPlayer(targetId);
		WarBet oldBet = warWorldData.betGet(uid);

		if (warPlayer == null || !warWorldData.isBet() || warWorldData.getCamType() != camType || (oldBet != null && oldBet.getC() == camType)) {
			map.put("ret", false);
		} else {

			WarBet warBet = warWorldData.betAsset(camType, serverId, uid, targetId, asset);
			warWorldData.commitSync();
			map.put("ret", true);
			map.put("betTarget", warBet);
			map.put("betName", warPlayer.getPn());
			map.put("betSid", warPlayer.getSid());
		}
		map.put("canBet", warWorldData.isBet());

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldBetList(Session session, String sign, long uid) {
		WarWorldData warWorldData = WarCoreCenter.getWarWorld();
		Iterator<WarBet> bets = warWorldData.getBets();
		List<Object[]> myBets = new ArrayList<Object[]>();
		while (bets.hasNext()) {
			WarBet warBet = (WarBet) bets.next();
			if (warBet.getS() == uid) {
				WarPlayer target = warWorldData.getWarPlayer(warBet.getT());
				myBets.add(new Object[] { warBet.getC(), warBet.getT(), target.getPn(), warBet.getAsset(), warBet.getW(), target.getSid() });
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bets", myBets);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldApiCam(Session session, String sign, int step, int camType) {
		CrossProxySerialize.getInstance().stateWar(2, step, camType);
		CrossProxySerialize.getInstance().commitSync();
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage worldAward(Session session, String sign, long uid, int camType) {
		Player target = Context.getPlayerServer().getByPlayerId(uid);
		if (target == null) {
			return ResponseMessage.getSuccMessage(true);
		}
		if (!CrossProxySerialize.getInstance().awardAddWorld(uid, camType)) {
			LogUtil.getMain().debug("world haved " + uid + " | " + camType);
			return ResponseMessage.getSuccMessage(true);
		}
		int[] award = WarConfig.awardMap.get(camType);
		int aNum = award[4];
		int zgNum = award[5];
		int hId = WarConfig.getCTI(camType);

		CrossProxySerialize.getInstance().worldHIdAdd(uid, hId);// 增加称号记录
		CrossProxySerialize.getInstance().commitSync();

		target.alterAsset(aNum);
		target.getDataInfo().alterAchive(zgNum);
		target.commitSync();
		target.getDataInfo().commitSync();
		// 广播银两变化
		Context.getPlayerServer().change(PlayerChangeVO.PLAYER_ASSET, target.getAsset(), uid);

		String title = LanguageLib.get("war.award.title3");
		String content = LanguageLib.get("war.award.content3", WarConfig.getName(camType), aNum, zgNum);

		Context.getMessageServer().sendSysMail(target.getPlayerId(), target.getPlayerName(), title, content);
		LogServer.recordLog(target, ConstantsLog.activity, 90303, 0, aNum, camType, 3);
		LogUtil.getMain().debug("world award " + uid + " c " + camType);

		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campFirst(Session session, String sign, int sid, long uid) {
		Map<String, Object> map = new HashMap<String, Object>();
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);
		boolean join = campCrossArea.isJoin(uid);
		map.put("join", join);
		if (join) {
			CampTeam campTeam = campCrossArea.getCampTeam(uid);
			map.put("crossTeam", campTeam.toCampVO());
		}
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campJoin(Session session, String sign, int sid, CampTeam campTeam) {
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);
		if (campCrossArea.isOpen()) {
			return ResponseMessage.getSuccMessage(String.valueOf(-2));
		}
		long uid = campCrossArea.havedJoin(campTeam.getUids());
		if (uid == 0) {
			campCrossArea.join(campTeam);
			return ResponseMessage.getSuccMessage(String.valueOf(-1));
		} else {
			return ResponseMessage.getSuccMessage(String.valueOf(uid));

		}
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campShow(Session session, String sign, int sid, long uid) {
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);

		int imgLeft = campCrossArea.getNameIndexLeft();
		int imgRight = campCrossArea.getNameIndexRight();
		String nameLeft = LangConfigLib.getByIndex("config.campcross.read", imgLeft);
		String nameRight = LangConfigLib.getByIndex("config.campcross.blue", imgRight);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("masterId", campCrossArea.getMasterId(uid));
		map.put("imgLeft", imgLeft);
		map.put("imgRight", imgRight);
		map.put("nameLeft", nameLeft);
		map.put("nameRight", nameRight);
		map.put("allPlayers", campCrossArea.getAllPlayers());
		map.put("endTime", CampCrossCore.getEndTime());
		map.put("records", campCrossArea.recordsView(60));
		map.put("topPlayers", campCrossArea.getTop3());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campLog(Session session, String sign, int sid, long uid) {
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);
		Map<String, Object> map = new HashMap<String, Object>();
		long mastetId = campCrossArea.getMasterId(uid);
		map.put("records", campCrossArea.recordsView(mastetId, 30));
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campTeam(Session session, String sign, int sid, long masterId) {
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);
		CampTeam campTeam = campCrossArea.getCampTeam(masterId);
		if (campTeam == null) {
			return ResponseMessage.getSuccMessage(false);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("team", campTeam.toCampVO());

		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campTeamList(Session session, String sign, int sid) {
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);
		Collection<CampTeam> joinTeams = campCrossArea.getTeams();
		List<TeamCampVO> lists = new ArrayList<TeamCampVO>();
		for (CampTeam campTeam : joinTeams) {
			lists.add(campTeam.toCampVO());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", lists);
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campOrder(Session session, String sign, int sid) {
		CampCrossArea campCrossArea = CampCrossArea.getBySid(sid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", campCrossArea.getOrders());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campRecordsNotify(Session session, String sign, CampCrossReport report) {
		try {
			CampCrossCore.prossReport(report);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campBattle(Session session, String sign, CampCrossReportBattle reportBattle) {
		try {
			long[] ids = reportBattle.getUids();
			for (long uid : ids) {
				if (DataStaticCenter.campCrossOpen(uid)) {
					Context.getTcpServer().broadcast(reportBattle.getBattleReport(), ConstantsRadio.battle, uid);
				} else {
				}
				Player target = Context.getPlayerServer().getByPlayerId(uid);
				if (target != null) {
					int[] award = reportBattle.getAward();
					target.alterExp(award[0]);
					target.alterAsset(award[1]);
					target.commitSync();
						PlayerReceiveVO receive = new PlayerReceiveVO();
						receive.put(PlayerReceiveVO.RECEIVE_ASSET, award[1]);
						receive.put(PlayerReceiveVO.RECEIVE_EXP, award[0]);
						receive.broadcast(target.getPlayerId());
				//记录日志
				LogServer.recordLog(target, ConstantsLog.battle, 40404, reportBattle.isWin(),award[0],award[1]);
				} else {
					LogUtil.getMain().error("err uid " + uid);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campAward(Session session, String sign, String idstr, int awardId) {
		try {

			long[] ids = StringProcesser.toLongArray(idstr);
			SysModeAwards sysAwards = SysModeAwards.get(1003, awardId);
			if (sysAwards == null) {
				LogUtil.getMain().error("award err " + awardId);
				return ResponseMessage.getSuccMessage(false);
			}
			String title = LanguageLib.get("warpvp.award.title");
			String content = LanguageLib.get("warpvp.award.content");
			for (long uid : ids) {
				Player target = Context.getPlayerServer().getByPlayerId(uid);
				if (target != null) {
					List<SubPops> currentPops = sysAwards.makeAttachment();
					Context.getMessageServer().sendSysMail(target.getPlayerId(), target.getPlayerName(), title, content, currentPops);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage campState(Session session, String sign, boolean open) {
		DataStaticCenter.campCrossState(open);
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage actCross(Session session, String sign, MidAutumnParams params) {
		if (params.getSid() == ConstantsConfig.getSysCgServiceId()) {
			return ResponseMessage.getSuccMessage(true);
		}
		Collection<Long> ids = SessionHolder.getInstance().onlineIds();
		MidAutumnData.awardOthers(ids, params);
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage mmFlowerCross(Session session, String sign, MMFlowerLog mmFlower) {
		if (mmFlower.getN() >= 99) {//99朵以上才做记录
			MMFlowerData.getInstance().addMMFLowerLog(mmFlower);
		}
		MMFlowerData.getInstance().mmFlowerAdd(mmFlower.getN(), mmFlower.getMid());
		MMFlowerData.getInstance().commitSync();
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage mmFlowerLog(Session session, String sign) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fLog", MMFlowerData.getInstance().getMMFlowerLog());
		map.put("mfn", MMFlowerData.getInstance().getMMFlower());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage mmFlowerPlayer(Session session, String sign) {
		return ResponseMessage.getSuccMessage(MMFlowerData.getInstance().getTopList());
	}

	@UnAuth
	@GmCommand
	public ResponseMessage mmFlowerTop(Session session, String sign) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sRanking", MMFlowerData.getInstance().getTops());
		return ResponseMessage.getSuccMessage(map);
	}

	@UnAuth
	@GmCommand
	@AccessClose
	public ResponseMessage mmFlowerAward(Session session, String sign, long uid, int orderId) {

		Player player = Context.getPlayerServer().getByPlayerId(uid);
		if (player == null) {
			ResponseMessage.getSuccMessage(false);
		}
		int titleId = orderId <= 0 ? 0 : orderId <= 3 ? 66 + orderId : orderId <= 10 ? 70 : 0;
		if (titleId > 0) {
			Context.getGeneralServer().addTitle(uid, titleId);
		}
		
		int aTypeId = orderId == 1 ? 1 : orderId == 2 ? 2 : orderId == 3 ? 3 : 4;
		SysModeAwards sysAwards = SysModeAwards.get(104, aTypeId);
		if (sysAwards == null) {
			ResponseMessage.getSuccMessage(false);
		}
		String title = LanguageLib.get("holiday.mmflower.title2");
		String content = LanguageLib.get("holiday.mmflower.comtent2", orderId);
		Context.getMessageServer().sendSysMail(uid, player.getPlayerName(), title, content, sysAwards.makeAttachment());
		LogServer.recordLog(player, ConstantsLog.activity, 90473, orderId, sysAwards.getTitle());
		return ResponseMessage.getSuccMessage(true);
	}
	
	/**
	 * 放烽火
	 * @param session
	 * @param sign
	 * @param sid
	 * @param uid
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage balefireSend(Session session,String sign,int sid,long uid,String name){
		return ResponseMessage.getSuccMessage(BalefileStaticeData.crossAdd(sid,uid,name));
	}
	@UnAuth
	@GmCommand
	public ResponseMessage balefireState(Session session,String sign){
		return ResponseMessage.getSuccMessage(BalefileStaticeData.getState());
	}
	@UnAuth
	@GmCommand
	public ResponseMessage balefireAward(Session session,String sign,String idstr,String ticp){
		BalefileStaticeData.prossAwar(idstr,ticp);
		return ResponseMessage.getSuccMessage(true);
	}	

}
